var curr="1";
var pageSize="10";
var table;
var layer;
$(function () {
    sessionStorage.setItem("filePath",sessionStorage.getItem("userId"));
    //获取表格数据
    getAllFilesList(0,10);

})
/**
 * 后台返回数据填充表格 demo
 */
function getAllFilesList(currt,size) {
    var obsFile={};
    obsFile.userId=sessionStorage.getItem("id");
    obsFile.fileState=1;
    if(sessionStorage.getItem("filePath")!=""){
        obsFile.path=sessionStorage.getItem("filePath")+"/";
    }
    obsFile.type="文件夹";
    var page={};
    page.currt=currt;
    page.size=size;
    var obj={};
    obj.obsFile=obsFile;
    obj.page=page;
    $.ajax({
        url: sessionStorage.getItem("rootPath") + "/files/slectFileList",
        method: "post",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(obj
        ),
        success: function (res) {
            //登录失效，跳转到登录页面
            console.log(res);
            if(res.status == -1){
                window.top.location.href = 'login.html';
            }
            renderTable(res);
            renderpage(res);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
        }
    })
}

/**
 * 表格渲染
 */
function renderTable(data) {
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#allFilesTable'
            ,url: '../../admin/data/common.json'
            , page: false
            , skin:'line'
            , height: 350
            ,toolbar: '#toolbar' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: []
            , parseData: function (res) {
                return {
                    "code": 0,
                    "msg": "",
                    "data": data.data
                }
            }
            , cols: [[
                {type:'checkbox',width:30},
                {field: 'type',width:70,title: '图标', templet:function (data) {
                        if(data.type=="文件夹"){
                            return '<img src="admin/images/tag_folder.png" style="width:30px;"/>';
                        }
                    }}
                , {field: 'fileName', title: '名称', templet: function (data) {
                        return "<a href='javascript:void(0)' style='color: #1aa5fb'"+" onclick=openfile('" + data.path + "','"+data.type+"')>" + data.fileName + "</a>";
                    }}
            ]]
        });
    });
}
/**
 * 分页
 */
function renderpage(date) {
    layui.use('laypage', function () {
        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
            elem: 'allFilesTable_page' //注意，这里的 test1 是 ID，不用加 # 号
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
            , count: date.total //数据总数，从服务端得到
            , limits: [10]
            , curr: curr
            , limit: pageSize
            , jump: function (obj, first) {
                //首次不执行
                if (!first) {
                    curr = obj.curr;
                    pageSize = obj.limit;
                    getAllFilesList(curr-1,pageSize);
                }
            }
        });

    });
}
function returnParentFolder(){
    var obsFile={};
    obsFile.userId=sessionStorage.getItem("id");
    obsFile.path=sessionStorage.getItem("parentPath");
    obsFile.fileState=1;
    obsFile.type="文件夹";
    var page={};
    page.currt=0;
    page.size=10;
    var obj={};
    obj.obsFile=obsFile;
    obj.page=page;
    var p=sessionStorage.getItem("parentPath");
    p=p.substring(0,p.length-1);
    sessionStorage.setItem("filePath",p);
    $.ajax({
        url: sessionStorage.getItem("rootPath") + "/files/slectFileList",
        method: "post",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(obj),
        success: function (res) {
            var path = obsFile.path;
            path = path.substring(0,path.length-1);
            curr=1;
            while (path.charAt(path.length-1)!='/'){
                path=path.substring(0,path.length-1);
                if(path==""){
                    break;
                }
            }
            if(path!=""){
                sessionStorage.setItem("parentPath",path);

            }

            renderTable(res);
            renderpage(res);

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
        }
    })
}



function openfile(path,type){
    var obsFile={};
    obsFile.userId=sessionStorage.getItem("id");
    obsFile.path=path+"/";
    obsFile.fileState=1;
    obsFile.type=type;
    var page={};
    page.currt=0;
    page.size=10;
    var obj={};
    obj.obsFile=obsFile;
    obj.page=page;
    $.ajax({
        url: sessionStorage.getItem("rootPath") + "/files/slectFileList",
        method: "post",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(obj),
        success: function (res) {
            if (res.total!=0) {
                sessionStorage.setItem("filePath", path);
                while (path.charAt(path.length - 1) != '/') {
                    path = path.substring(0, path.length - 1);
                }
                sessionStorage.setItem("parentPath", path);
                renderTable(res);
                renderpage(res);
            }else {
                layer.msg("没有文件夹咯");
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
        }
    })
}

/**

 * 返回选择的数据

 */


var callbackData =function () {
    var checkStatus=table.checkStatus("allFilesTable");
    if(checkStatus.data.length!=1){
        layui.use('layer', function() { //独立版的layer无需执行这一句
            var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
            layer.alert('只能选择一个文件夹！');
        })
    }else{
            var data = checkStatus.data[0].path;
    }
    return data;
};